package com.acce.gxsc.page.registerCar.selectGoods;

import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.GoodsBean;
import com.acce.gxsc.model.netBean.GoodsListBean;
import com.acce.sdk.base.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by huxley on 2017/5/26.
 */

public class SelectGoodsPresenter extends BasePresenter<SelectGoodsView> {

    public void loadGoodsList() {
        WorkModel.getInstance().loadGoodsList().map(new Func1<ResultBean<GoodsListBean>, List<GoodsBean>>() {
            @Override
            public List<GoodsBean> call(ResultBean<GoodsListBean> result) {
                for (int i = 0; i < result.result.GoodsType.size(); i++) {
                    GoodsBean goodsBean = result.result.GoodsType.get(i);
                    goodsBean.child = new ArrayList<>();
                    for (int j = 0; j < result.result.GoodsName.size(); j++) {
                        GoodsBean child = result.result.GoodsName.get(j);
                        if (child.fatherId.equals(goodsBean.goodsId)) {
                            goodsBean.child.add(child);
                        }
                    }
                }
                return result.result.GoodsType;
            }
        }).subscribe(new Subscriber<List<GoodsBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(List<GoodsBean> datas) {
                if (mViewRef != null) {
                    getView().loadSuccess(datas);
                }
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().hideProgressDialog();
                }
            }
        });
    }
}
